/**
  SUNNY Project, Anyang Normal University, IMP-CAS
  \class TAPicMat
  \brie visualize the matrix in a picture. Mainly this is a class to show the
  structure of a large sparse matrix, which is not eligible to show in a terminal
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2022/11/28
  \date Last modified: 2022/11/28 by SUN Yazhou
  \copyright 2020-2023 SUN Yazhou
  \copyright SUNNY project, Anyang Normal University, IMP-CAS
*/

#include "TAPicMat.h"
#include "TAException.h"

#ifdef _OpenCV_
#include <opencv2/imgcodecs/legacy/constants_c.h>

using cv::Vec3b;
using cv::waitKey;


std::vector<int> TAPicMat::REALPNG{
  CV_IMWRITE_PNG_COMPRESSION,
  9,
  cv::IMWRITE_PNG_STRATEGY,
  cv::IMWRITE_PNG_STRATEGY_DEFAULT
};

TAPicMat::TAPicMat(int nr, int nc) : Mat(nr, nc, CV_8UC3){}

void TAPicMat::SetPixelWhite(int r, int c){
  at<Vec3b>(r, c)[0] = 0xFF;
  at<Vec3b>(r, c)[1] = 0xFF;
  at<Vec3b>(r, c)[2] = 0xFF;
} // end member function SetPixelWhite

void TAPicMat::SetPixelGreen(int r, int c){
  at<Vec3b>(r, c)[0] = 0x00;
  at<Vec3b>(r, c)[1] = 0x00;
  at<Vec3b>(r, c)[2] = 0xFF;
} // end member function SetPixelWhite

void TAPicMat::Show(const string &name){
  imshow(name, (*this)); // open a floating windown to show the picture
  waitKey(); // press any key to close this picture
  imwrite(name+".png", (*this), REALPNG);
} // end member function Show

#else

std::vector<int> TAPicMat::REALPNG;

TAPicMat::TAPicMat(int nr, int nc){
  TAException::Error("TAPicMat", "SetPixelWhite: OpenCV not installed.");
} // end ctor

void TAPicMat::SetPixelWhite(int r, int c){
  TAException::Error("TAPicMat", "SetPixelWhite: OpenCV not installed.");
} // end member function SetPixelWhite

void TAPicMat::SetPixelGreen(int r, int c){
  TAException::Error("TAPicMat", "SetPixelGreen: OpenCV not installed.");
} // end member function SetPixelWhite

void TAPicMat::Show(const string &name){
  TAException::Error("TAPicMat", "Show: OpenCV not installed.");
} // end member function Show

#endif

TAPicMat::~TAPicMat(){}
